package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.adapters.GroupListAdapter;
import com.chuanye.jiguangimdemo.controller.ActivityController;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetGroupIDListCallback;
import cn.jpush.im.android.api.callback.GetGroupInfoCallback;
import cn.jpush.im.android.api.model.GroupInfo;

/**
 * 群组验证页面
 */
public class GroupActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "GroupActivity";
    private Context mContext;
    private ImageView group_back_iv;

    private ListView mGroupList;
    private GroupListAdapter mGroupListAdapter;
    private boolean isFromForward = false;
    private boolean isBusinessCard = false;
    private String mUserName;
    private String mAppKey;
    private String mAvatarPath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        mContext = this;
        ActivityController.addActivity(this);

        initView();
    }

    private void initView() {
        //返回按钮
        group_back_iv = findViewById(R.id.group_back_iv);
        group_back_iv.setOnClickListener(this);

        mGroupList = (ListView) findViewById(R.id.group_list_lv);


        //待发送名片的参数
        mUserName = getIntent().getStringExtra("userName");
        mAppKey = getIntent().getStringExtra("appKey");
        mAvatarPath = getIntent().getStringExtra("avatar");

        initData();

    }

    private void initData() {

        final List<GroupInfo> infoList = new ArrayList<>();
        JMessageClient.getGroupIDList(new GetGroupIDListCallback() {
            @Override
            public void gotResult(int responseCode, String responseMessage, final List<Long> groupIDList) {
                if (responseCode == 0) {
                    final int[] groupSize = {groupIDList.size()};
                    if (groupIDList.size() > 0) {
                        for (Long id : groupIDList) {
                            JMessageClient.getGroupInfo(id, new GetGroupInfoCallback() {
                                @Override
                                public void gotResult(int i, String s, GroupInfo groupInfo) {
                                    if (i == 0) {
                                        groupSize[0] = groupSize[0] - 1;
                                        infoList.add(groupInfo);
                                        if (groupSize[0] == 0) {
                                            setAdapter(infoList);
                                        }

                                    }
                                }
                            });
                        }
                    } else {
                        Toast.makeText(GroupActivity.this, "您还没有群组",Toast.LENGTH_SHORT).show();
                    }
                } else {

                    //HandleResponseCode.onHandle(mContext, responseCode, false);
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.group_back_iv://返回
                finish();
                break;
        }
    }


    @SuppressLint("WrongConstant")
    public void setAdapter(List<GroupInfo> infoList) {
        //来自转发时flag是1
        if (getIntent().getFlags() == 1) {
            isFromForward = true;
        }
        //来自名片的请求设置flag==2
        if (getIntent().getFlags() == 2) {
            isBusinessCard = true;
        }
        //mGroupListAdapter = new GroupListAdapter(mContext, infoList, isFromForward, mWidth, isBusinessCard, mUserName, mAppKey, mAvatarPath);
        //mGroupList.setAdapter(mGroupListAdapter);
    }
}
